package com.hjs.servlet.road;

import com.hjs.bean.*;
import com.hjs.mapper.*;
import com.hjs.tools.Mapper;
import com.hjs.tools.RequestUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author flamemaster
 * @date 2020/6/20
 **/
@Slf4j
@WebServlet("/road/search")
public class Search extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response){
        RequestUtil requestUtil = new RequestUtil(request, response);
        switch (Integer.parseInt(request.getParameter("searchType"))) {
            case 1:
                searchRoadBase(requestUtil);
                break;
            case 2:
                searchRoadway(requestUtil);
                break;
            case 3:
                searchRoadType(requestUtil);
                break;
            case 4:
                searchMaintenanceLevel(requestUtil);
                break;
            case 5:
                searchAffiliatedFacility(requestUtil);
                break;
            case 6:
                searchDividerOneSide(requestUtil);
                break;
            case 7:
                searchSidewalkOneSide(requestUtil);
                break;
            case 8:
                searchEvaluationALl(requestUtil);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + Integer.parseInt(request.getParameter("addType")));
        }
    }

    private void searchEvaluationALl(RequestUtil requestUtil) {
        Mapper<EvaluationResultsMapper> evaluationResultsMapperMapper = new Mapper<>(EvaluationResultsMapper.class);
        try {
            EvaluationResultsMapper mapper = evaluationResultsMapperMapper.getMapper();
            List<EvaluationResultView> allEvaluationResult = mapper.getAllEvaluationResult();
            requestUtil.responseWrite(Mapper.toJsonString(allEvaluationResult));
        } catch (Exception e) {
            e.printStackTrace();
            requestUtil.responseFail("未获取到年报表信息！");
        }finally {
            evaluationResultsMapperMapper.closeSession();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response){

    }

    private void searchRoadBase(RequestUtil requestUtil) {
        Mapper<RoadMapper> roadMapperMapper = new Mapper<>(RoadMapper.class);
        Mapper<RoadAuditMapper> roadAuditMapperMapper = new Mapper<>(RoadAuditMapper.class);
        try {
            RoadMapper mapper = roadMapperMapper.getMapper();
            RoadAuditMapper mapper1 = roadAuditMapperMapper.getMapper();
            List<RoadAuditView> roadAuditViews=new ArrayList<>();
//            superpower    1代表是总管理员   0代表是区域管理员
            if(requestUtil.getInt("superpower")==1){
                List<Integer> auditStatus = mapper1.getAllRoadIdAll();



                for (Integer status : auditStatus) {
                    roadAuditViews.add(new RoadAuditView(
                            mapper.getOneRoadById(status),
                            mapper1.getRoadAuditByRoadID(status)));


                }
            }else {
                List<Integer> auditStatus = mapper1.getAllRoadIdByDepartmentAll(requestUtil.getInt("departmentID"));

                for (Integer status : auditStatus) {
                    roadAuditViews.add(new RoadAuditView(mapper.getOneRoadById(status),
                            mapper1.getRoadAuditByRoadID(status)));
                }
            }
            requestUtil.responseWrite(Mapper.toJsonString(roadAuditViews));
        } catch (Exception e) {
            e.printStackTrace();
            requestUtil.responseWrite("{\"result\":false}");
        }finally {
            roadMapperMapper.closeSession();
            roadAuditMapperMapper.closeSession();
        }
    }

    private void searchRoadway(RequestUtil requestUtil) {
        Mapper<RoadwayMapper> roadwayMapperMapper = new Mapper<>(RoadwayMapper.class);
        try {
            RoadwayMapper mapper = roadwayMapperMapper.getMapper();
            Roadway roadway = mapper.getOneRoadwayById(requestUtil.getInt("roadwayID"));
            List<TypeAndLength> lengthList = mapper.getOneTypeAndLengthById(requestUtil.getInt("roadwayID"));
            List<TypeAndThickness> thicknessList = mapper.getOneTypeAndThicknessById(requestUtil.getInt("roadwayID"));
            RoadwayView roadwayView = new RoadwayView(roadway, lengthList, thicknessList);
            requestUtil.responseWrite(Mapper.toJsonString(roadwayView));
        } catch (Exception e) {
            requestUtil.responseFail("未获取到车行道信息");
        }finally {
            roadwayMapperMapper.closeSession();
        }
    }

    private void searchRoadType(RequestUtil requestUtil) {
        Mapper<RoadTypeMapper> roadTypeMapperMapper = new Mapper<>(RoadTypeMapper.class);
        try {
            RoadTypeMapper mapper = roadTypeMapperMapper.getMapper();
            List<RoadType> result = mapper.getALlRoadType();
            requestUtil.responseWrite(Mapper.toJsonString(result));
        } catch (Exception e) {
            requestUtil.responseWrite("{\"result\":false}");
        }finally {
            roadTypeMapperMapper.closeSession();
        }
    }

    private void searchMaintenanceLevel(RequestUtil requestUtil) {
        Mapper<RoadTypeMapper> roadTypeMapperMapper = new Mapper<>(RoadTypeMapper.class);
        try {
            RoadTypeMapper mapper = roadTypeMapperMapper.getMapper();
            List<MaintenanceLevel> result = mapper.getALlMaintenanceLevel();
            requestUtil.responseWrite(Mapper.toJsonString(result));
        } catch (Exception e) {
            requestUtil.responseWrite("{\"result\":false}");
        }finally {
            roadTypeMapperMapper.closeSession();
        }
    }

    private void searchAffiliatedFacility(RequestUtil requestUtil) {
        Mapper<RoadMapper> roadMapperMapper = new Mapper<>(RoadMapper.class);
        try {
            RoadMapper mapper = roadMapperMapper.getMapper();
            AffiliatedFacility result = mapper.getOneAffiliatedFacilityById(requestUtil.getInt("affiliatedFacilityID"));
            requestUtil.responseWrite(Mapper.toJsonString(result));
        } catch (Exception e) {
            requestUtil.responseFail("未获取到附属设施信息");
        }finally {
            roadMapperMapper.closeSession();
        }
    }

    private void searchDividerOneSide(RequestUtil requestUtil) {
        Mapper<RoadMapper> roadMapperMapper = new Mapper<>(RoadMapper.class);
        try {
            RoadMapper mapper = roadMapperMapper.getMapper();
            List<DividerOneSide> result = mapper.getDividerOneSideById(requestUtil.getInt("roadID"));
            requestUtil.responseWrite(Mapper.toJsonString(result));
        } catch (Exception e) {
            requestUtil.responseWrite("{\"result\":false}");
        }finally {
            roadMapperMapper.closeSession();
        }
    }

    private void searchSidewalkOneSide(RequestUtil requestUtil) {
        Mapper<RoadMapper> roadMapperMapper = new Mapper<>(RoadMapper.class);
        try {
            RoadMapper mapper = roadMapperMapper.getMapper();
            List<SidewalkOneSide> result = mapper.getSidewalkById(requestUtil.getInt("roadID"));
            requestUtil.responseWrite(Mapper.toJsonString(result));
        } catch (Exception e) {
            requestUtil.responseWrite("{\"result\":false}");
        }finally {
            roadMapperMapper.closeSession();
        }
    }

}
